package com.cn.view;

import com.cn.bean.Goods;
import com.cn.controller.Select;
import com.cn.controller.Updata;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GoodsManage extends JFrame {
    private JTextField textField;
    static Select select = new Select();
    Updata updata = new Updata();
    static Object[] header= {"商品编号","商品名称","商品单价","商品库存","商品图片"};
    static String sql = "SELECT goodsID,goodsName,goodsPrice,goodsCount,goodsImg FROM goods_goods";
    static Object[][] data= select.getGoods(sql);
    public static DefaultTableModel df = new DefaultTableModel(data, header);
    int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

    public GoodsManage() {
        super("超市信息管理系统");
        this.setBounds(0, 0, 700, 450);
        this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
        this.setResizable(false);//让窗口大小不可改变
        getContentPane().setLayout(null);

        JTable jTable = new JTable(df);
        JScrollPane jsp=new JScrollPane(jTable,v,h);
        jsp.setBounds(10, 10, 515, 320);
        getContentPane().add(jsp);

        JButton button_1 = new JButton("显示所有商品");
        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sql = "SELECT goodsID,goodsName,goodsPrice,goodsCount,goodsImg FROM goods_goods";
                Object[][] data = Select.getGoods(sql);
                df.setDataVector(data, header);
            }
        });

        button_1.setBounds(535, 80, 127, 30);
        getContentPane().add(button_1);

        JButton button_2 = new JButton("修改商品");
        button_2.setBounds(535, 140, 127, 30);
        getContentPane().add(button_2);
        button_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jTable.getSelectedColumn()<0) {
                    JOptionPane.showMessageDialog(null, "请选择要修改的数据！");
                } else {
                    int goodsID = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
                    String name = jTable.getValueAt(jTable.getSelectedRow(), 1).toString();
                    int num = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 2).toString());
                    String price = jTable.getValueAt(jTable.getSelectedRow(), 3).toString();
                    String img = jTable.getValueAt(jTable.getSelectedRow(),4).toString();
                    Goods goods = new Goods(goodsID,name,price,num,img);
                    GoodsXG goodsXG = new GoodsXG(goods);
                    goodsXG.setVisible(true);
                    refresh();
                }

            }
        });

        JButton button_3 = new JButton("删除商品");
        button_3.setBounds(535, 200, 127, 30);
        getContentPane().add(button_3);
        button_3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jTable.getSelectedColumn()<0) {
                    JOptionPane.showMessageDialog(null, "请选中要删除的数据！");
                } else {
                    int goodsID = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
                    String sql="delete from goods_goods where goodsID="+goodsID;
                    int result = updata.addData(sql);
                    if (result>0) {
                        JOptionPane.showMessageDialog(null, "删除成功！");
                        refresh();
                        //JOptionPane.showMessageDialog(null, "记得刷新一下哦！");
                    } else {
                        JOptionPane.showMessageDialog(null, "删除失败！");
                    }
                }
            }
        });

        JButton button_4 = new JButton("添加商品");
        button_4.setBounds(535, 258, 127, 30);
        getContentPane().add(button_4);
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                GoodsADD goodsAdd = new GoodsADD();
                goodsAdd.setVisible(true);
            }
        });

        JLabel label = new JLabel("商品编号：");
        label.setBounds(40, 354, 112, 32);
        getContentPane().add(label);

        textField = new JTextField();
        textField.setBounds(154, 358, 127, 26);
        getContentPane().add(textField);
        textField.setColumns(10);

        JButton button = new JButton("按编号查询");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String sql = "SELECT goodsID,goodsName,goodsPrice,goodsCount,goodsImg FROM goods_goods WHERE goodsID LIKE '%"+textField.getText()+"%'";
                Object[][] data = Select.getGoods(sql);
                df.setDataVector(data, header);
            }
        });
        button.setBounds(305, 355, 112, 30);
        getContentPane().add(button);

        this.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                //加入动作
                GoodsManagement m = new GoodsManagement();
                m.setVisible(true);
            }
        });


    }

    public static void refresh() {
        String sql = "SELECT goodsID,goodsName,goodsPrice,goodsCount,goodsImg FROM goods_goods";
        Object[][] data = Select.getGoods(sql);
        df.setDataVector(data, header);
    }



    public static void main(String[] args) {
        GoodsManage t = new GoodsManage();
        t.setVisible(true);
    }
}
